//by Navy Zhang 710605420@qq.com
package com.tencent.memoryuse;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.tencent.memoryuseLibrary.MemoryHelper;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {
    private Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MemoryHelper.Init(getBaseContext());



        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // (1) 使用handler发送消息
                Message message=new Message();
                message.what=0;
                mHandler.sendMessage(message);
            }
            },0,1000);//每隔一秒使用handler发送一下消息,也就是每隔一秒执行一次,一直重复执行
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 0){
                System.out.println("------------------ memory ------------------ ");
                System.out.println("total memory: "+MemoryHelper.GetMemoryXmsTotalUsage()* 1.0f/ (1024 * 1024));
                System.out.println("free memory: "+MemoryHelper.GetMemoryXmsFree()* 1.0f/ (1024 * 1024));
                System.out.println("max memory: "+MemoryHelper.GetMemoryXmsMax()* 1.0f/ (1024 * 1024));
                System.out.println("native total memory: "+MemoryHelper.GetNativeAllocatedSize()* 1.0f/ (1024 * 1024));
                System.out.println("native free memory: "+MemoryHelper.GetNativeFreeSize()* 1.0f/ (1024 * 1024));
                System.out.println("native max memory: "+MemoryHelper.GetNativeMaxSize()* 1.0f/ (1024 * 1024));
                System.out.println("Process memory: "+MemoryHelper.GetProcessPss()* 1.0f/ ( 1024));

                System.out.println("dalvik has used memory: "+(MemoryHelper.GetMemoryXmsTotalUsage() - MemoryHelper.GetMemoryXmsFree()) *1.0f /(1024 * 1024) +"MB");
                System.out.println("native has used memory: "+(MemoryHelper.GetNativeAllocatedSize() - MemoryHelper.GetNativeFreeSize())*1.0f /(1024 * 1024) +"MB");
                System.out.println("------------------ memory ------------------ ");
            }
        }
    };
}
